package com.xu.mapper;

import com.xu.pojo.Journal;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface JournalMapper {

    @Select("select users.username,journal.action,journal.date,journal.type from bread.journal join bread.users where journal.id=users.uid order by journal.date desc")
    List<Journal> queryAllJournal();

    @Insert("insert into bread.journal (id,date,action,type) " +
            "values (#{id},#{date},#{action},#{type})")
    int insertJournal(Journal journal);

    @Select("select count(*) from journal WHERE DATE(date) = CURDATE()")
    int todayGet();

    @Select({
            "select users.username,journal.action,journal.date,journal.type from bread.journal join bread.users",
            "WHERE 1=1",
            "AND (",
            "    CASE WHEN #{start} IS NOT NULL AND #{length} IS NOT NULL END",
            ")"
    })
    List<Journal> journalPage(@Param("start") Integer start, @Param("length") Integer length);

}
